
/**
* -----Tag folder-----
* http://code.google.com/p/tagfolder
* Licensed under LGPL
* chuanshenglu@gmail.com
* xrjieyee@gmail.com
*
*/

#include "client_stub_impl.h"
#include "TFNamedPipe.h"
#include <stdio.h>
#include <windows.h>
#include <TCHAR.h>
#include <Tlhelp32.h>
#include "TFDeamon.h"


bool SendData(void *data,unsigned long size)
{
	TFNamedPipe pipe;
	if( pipe.Put(data,size) )
	{
		return true;
	}
	return false;
}

bool SendCommand(TFMessage &msg)
{
	return SendData(&msg,sizeof(msg));
}

TFMessage ReceiveCommand()
{
	TFNamedPipe pipe;
	TFMessage msg;
	pipe.Get( &msg, sizeof(msg));
	return msg;
}

std::string ReceiveString()
{
	TFNamedPipe pipe;
	return pipe.GetString();
}

DWORD GetProcessIdFromName(const TCHAR *name)
{
	HANDLE hSnapshot= CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,NULL);
	if(hSnapshot==NULL){
		MessageBox(HWND_DESKTOP,TEXT("Fail to get snapshot"),TEXT("Process"),MB_OK);
		return -1;
	}

	PROCESSENTRY32 entry;
	entry.dwSize=sizeof(PROCESSENTRY32);
	if(FALSE==Process32First(hSnapshot,&entry))
	{
		return -2;
	}
	while(Process32Next(hSnapshot,&entry)){
		TCHAR *p=NULL;
#ifdef UNICODE
		p=StrStr(entry.szExeFile,name);
#else
		p=strstr(entry.szExeFile,name);
#endif
		if(p!=NULL){
			return entry.th32ProcessID;
		}
	}
	return -1;
}
